package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {


    @Override
    public ResponseResult saveApAuthor(ApAuthor apAuthor) {
        if(apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        apAuthor.setCreatedTime(new Date());
        boolean b = this.save(apAuthor);
        if(b){
            return ResponseResult.okResult((AppHttpCodeEnum.SUCCESS));
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ApAuthor findByUserId(Integer userId) {
        if(userId==null){
            return null;
        }
        List<ApAuthor> apAuthorList = this.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
        if(apAuthorList.size()>0){
            return apAuthorList.get(0);
        }
        return null;
    }

    @Override
    public ApAuthor findByName(String name) {
        if(StringUtils.isBlank(name)){
            return null;
        }
        ApAuthor apAuthor = this.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        return apAuthor;
    }

    @Override
    public ApAuthor findByWmUserId(Integer userId) {
        if(userId==null){
            return null;
        }
        List<ApAuthor> apAuthorList = this.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, userId));
        if(apAuthorList.size()>0){
            return apAuthorList.get(0);
        }
        return null;
    }
}
